package DEMO;
//场景是创建两个线程对同一个数分别进行5w次的自增，预期结果是让这个数最后的结果返回一个10w
////用synchronized 锁住整个方法
public class demo_1 {
    public static void main(String[] args) throws InterruptedException {
        Counter1 counter=new Counter1();
        Thread thread1 =new Thread(()->{
            for (int i = 0; i <5_0000 ; i++) {
                counter.increase();
            }
        });
        Thread thread2 =new Thread(()->{
            for (int i = 0; i <5_0000 ; i++) {
                counter.increase();
            }
        });
        //启动线程
        thread1.start();
        thread2.start();
        //等待线程执行完成
        thread1.join();
        thread2.join();
        //打印结果
        System.out.println("count= "+counter.count);
    }
}
//创建一个类用来累加
class Counter1{
    public int count=0;
    //用synchronized 锁住整个方法
    public synchronized void increase(){
            count++;

    }
}
